package mypazar.customer;

import jason.environment.grid.Location;

import java.util.List;
import java.util.Vector;

import mypazar.jenamodel.PazarVocabulary;
import mypazar.pazar.Stall;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class CustomerBeliefBaseOperations {

	/**
	 * @param beliefBase
	 * @return
	 */
	public List<Resource> getStallsOnSamePosition(Model beliefBase) {
		String queryString = "PREFIX pazar: <http://example.com/> "
				+ "SELECT * " + "WHERE {  ?pazar pazar:person ?person. "
				+ "?person pazar:position ?position. "
				+ "?person pazar:stall ?stall."
				+ "?position pazar:posx ?posx. "
				+ "?position pazar:posy ?posy. " + "?stall pazar:posx ?posx. "
				+ "?stall pazar:posy ?posy. " + "?stall pazar:seen ?seen."
				+ "      }";

		Query query = QueryFactory.create(queryString);
		QueryExecution qExec = QueryExecutionFactory.create(query, beliefBase);
		ResultSet results = qExec.execSelect();
		List<Resource> stallsOnSamePosition = new Vector<Resource>();

		while (results.hasNext()) {
			stallsOnSamePosition.add(results.next().getResource("stall"));
		}

		qExec.close();
		return stallsOnSamePosition;
	}

	/**
	 * @param beliefBase
	 * @return
	 */
	public List<Resource> getStallsOnDifferentPosition(Model beliefBase) {
		String queryString = "PREFIX pazar: <http://example.com/> "
				+ "SELECT * " + "WHERE {  ?pazar pazar:person ?person. "
				+ "?person pazar:position ?position. "
				+ "?person pazar:stall ?stall."
				+ "?position pazar:posx ?posx. "
				+ "?position pazar:posy ?posy. " + "?stall pazar:posx ?posx. "
				+ "?stall pazar:posy ?posy. " + "      }";

		Query query = QueryFactory.create(queryString);
		QueryExecution qExec = QueryExecutionFactory.create(query, beliefBase);
		ResultSet results = qExec.execSelect();
		List<Resource> stallsOnDifferentPosition = new Vector<Resource>();

		while (results.hasNext()) {
			stallsOnDifferentPosition.add(results.next().getResource("stall"));
		}

		qExec.close();

		return stallsOnDifferentPosition;
	}

	/**
	 * Retrieves the position property of the given person resource.
	 * 
	 * @param resPerson
	 *            person resource.
	 * @return position individual.
	 * @throws Exception
	 */
	public Resource getPersonLocationNode(Resource resPerson) throws Exception {
		// get position property of agent...
		StmtIterator iterPerson = resPerson.listProperties(ResourceFactory
				.createProperty(PazarVocabulary.POSITION_URI));

		if (iterPerson.hasNext()) {
			Statement stmtPersonLoc = iterPerson.next();
			Resource resPersonLoc = stmtPersonLoc.getObject().asResource();
			return resPersonLoc;
		}

		throw new Exception("Position node is not found!");
	}

	/**
	 * Gets lowest product price from given belief base
	 * 
	 * @param personURI
	 * @param beliefBase
	 * @param acFindLowerPrice
	 * @return
	 */
	public Stall getLowestPrice(String personURI, Model beliefBase) {
		int temp = 100;
		Location productLoc = new Location(0, 0);
		String smanName = null;
		int price = 0;

		Resource resPerson = beliefBase.getResource(personURI);

		StmtIterator iterReceived = resPerson.listProperties(ResourceFactory
				.createProperty(PazarVocabulary.RECEIVED_PRICE_URI));

		while (iterReceived.hasNext()) {
			Statement stmtReceivedPrice = iterReceived.next();
			Resource resReceivedPrice = stmtReceivedPrice.getObject()
					.asResource();

			price = resReceivedPrice
					.getProperty(
							ResourceFactory
									.createProperty(PazarVocabulary.PRICE_URI))
					.getLiteral().getInt();
			if (price < temp) {
				productLoc.x = resReceivedPrice
						.getProperty(
								ResourceFactory
										.createProperty(PazarVocabulary.SENDER_POSX_URI))
						.getLiteral().getInt();

				productLoc.y = resReceivedPrice
						.getProperty(
								ResourceFactory
										.createProperty(PazarVocabulary.SENDER_POSY_URI))
						.getLiteral().getInt();

				smanName = resReceivedPrice
						.getProperty(
								ResourceFactory
										.createProperty(PazarVocabulary.SENDER_URI))
						.getLiteral().getString();

				temp = price;
			}
		}

		Stall st = new Stall(productLoc.x, productLoc.y,
				Integer.parseInt(smanName.substring(smanName.length() - 1)));

		return st;
	}

}
